#pragma once

#include "kernel/agv/agv_entity.h"
#include "kernel/agv/i_agv_command.h"
#include "kernel/interfaces/Result.h"
#include <memory>
#include <unordered_map>
#include <string>
#include <vector>

namespace application {
namespace vehicle {

/**
 * @brief 车辆操作分派器
 * @details 负责持有并分派车辆操作策略
 *          内部管理 AgvEntity -> IAgvCommand 的映射
 *          根据车辆类型分派不同的命令实现
 */
class VehicleOperationDispatcher {
public:
    VehicleOperationDispatcher() = default;
    ~VehicleOperationDispatcher() = default;

    // 注册车辆命令实现（按车辆编号）
    void registerCommandImplementation(
        int agvNumber,
        std::shared_ptr<kernel::agv::IAgvCommand> commandImpl
    );

    // 注销车辆命令实现
    void unregisterCommandImplementation(int agvNumber);

    // 导航命令
    kernel::Result<void> moveToStationList(
        kernel::agv::AgvEntity& agv,
        const std::vector<std::string>& stations,
        const std::string& operation = ""
    );

    kernel::Result<void> moveToStation(
        kernel::agv::AgvEntity& agv,
        const std::string& sourceId,
        const std::string& targetId
    );

    kernel::Result<void> cancelTask(
        kernel::agv::AgvEntity& agv
    );

    kernel::Result<void> pauseTask(
        kernel::agv::AgvEntity& agv
    );

    kernel::Result<void> resumeTask(
        kernel::agv::AgvEntity& agv
    );

    // 硬件控制命令
    kernel::Result<void> controlJack(
        kernel::agv::AgvEntity& agv,
        bool raise
    );

    kernel::Result<void> startCharging(
        kernel::agv::AgvEntity& agv,
        const std::string& chargePoint
    );

    kernel::Result<void> stopCharging(
        kernel::agv::AgvEntity& agv
    );

    kernel::Result<void> controlDoor(
        kernel::agv::AgvEntity& agv,
        int doorId,
        bool open
    );

    kernel::Result<void> switchMap(
        kernel::agv::AgvEntity& agv,
        const std::string& mapName,
        const std::string& switchPoint
    );

    // 查询命令
    kernel::Result<void> requestCurrentMap(
        kernel::agv::AgvEntity& agv
    );

    // 检查车辆是否已注册命令实现
    bool hasCommandImplementation(int agvNumber) const;

    // 获取已注册的车辆数量
    size_t getRegisteredVehicleCount() const;

private:
    // 车辆编号 -> 命令实现的映射
    std::unordered_map<int, std::shared_ptr<kernel::agv::IAgvCommand>> m_commandMap;

    // 获取车辆对应的命令实现
    std::shared_ptr<kernel::agv::IAgvCommand> getCommandImplementation(int agvNumber) const;
};

} // namespace vehicle
} // namespace application
